package string;

public class StringPool {
    public static final String s = "ABC";
    public static final String ss = "ABCDEF";
    public static void main(String[] args) {
        /**
         * 字符串常量池
         * Java利用字符串常量池复用字符串
         */

        //复用：
        String s1 = "ABC";
        String s2 = "ABC";
        String s3 = "DEF";
        String s4 = "DEF";
        String s5 = "A"+"BC";
        String s6 = s1 + s3;
        System.out.println(s1 == s2);//true
        System.out.println(s1 == s);//true
        System.out.println(s1 == s5);//true

        //不复用：
        //动态字符串不复用（创建了新的对象）
        System.out.println(ss == s6);//false  字符串连接的结构不可以复用-----引用了不同的对象
        char[] chs = {'A','B','C'};
        String s7 = new String(chs);
        String s8 = new String("ABC");
        String s9 = "A";
        String s10 = "BC";
        String s11 = s9 + s10;
        //创建了型对象
        System.out.println(s1 == s7);//false
        System.out.println(s1 == s8);//false
        System.out.println(s1 == s11);//false

        String a1 = "ABC";
        String a2 = null;
        System.out.println(a1 + a2);
    }
}
